package com.huilian.iotbox.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.huilian.iotbox.admin.dao.StatsStoreDeviceTypeDao;
import com.huilian.iotbox.admin.service.StatsStoreDeviceTypeService;
import com.huilian.iotbox.data.dto.ConsumeRecordDto;
import com.huilian.iotbox.data.dto.GiftIncrementDto;
import com.huilian.iotbox.data.dto.StatsStoreDeviceTypeDto;
import com.huilian.iotbox.data.enums.ConsumeRecordTypeEnum;
import com.huilian.iotbox.data.po.StatsStoreDetail;
import com.huilian.iotbox.data.po.StatsStoreDeviceType;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: loong
 * @create: 2023-04-19 09:05
 **/
@Service
@Slf4j
public class StatsStoreDeviceTypeServiceImpl implements StatsStoreDeviceTypeService {
    @Resource
    private StatsStoreDeviceTypeDao statsStoreDeviceTypeDao;

    @Override
    public void statsYesterdayStoreDeviceType() {
        String startTime = DateTimeUtils.getYesterdayStartTime();
        String endTime = DateTimeUtils.getTodayEndTime();


        Map<String, StatsStoreDeviceType> statsDeviceStoreMap = new HashMap<>();

        /**购买订单*/
        List<StatsStoreDeviceType> statsStoreDeviceTypeList = statsStoreDeviceTypeDao.statsStatesStoreDeviceType(startTime, endTime);
        for (StatsStoreDeviceType item : statsStoreDeviceTypeList) {
            StatsStoreDeviceType statsStoreDeviceType = this.init(item);
            statsStoreDeviceType.setTurnover(item.getTurnover());
            statsStoreDeviceType.setTurnoverOnLine(item.getTurnoverOnLine());
            statsStoreDeviceType.setTurnoverOffline(item.getTurnoverOffline());
            statsStoreDeviceType.setTurnoverDouYin(item.getTurnoverDouYin());
            statsStoreDeviceType.setTurnoverMeiTuan(item.getTurnoverMeiTuan());
            statsStoreDeviceType.setHandlingCharge(item.getHandlingCharge());
            statsStoreDeviceType.setSettlementMoney(item.getSettlementMoney());
            statsStoreDeviceType.setPlatformSettlementMoney(item.getPlatformSettlementMoney());
            statsStoreDeviceType.setWxMoney(item.getWxMoney());
            statsStoreDeviceType.setAliMoney(item.getAliMoney());
            statsStoreDeviceType.setCashMoney(item.getCashMoney());
            statsStoreDeviceType.setOutCoinSales(item.getOutCoinSales());
            statsStoreDeviceType.setOutCoinGive(item.getOutCoinGive());
            statsStoreDeviceType.setSelfServiceTerminalCash(item.getSelfServiceTerminalCash());
            statsStoreDeviceType.setScanningFee(item.getScanningFee());
            statsStoreDeviceType.setTiktokCancellationFee(item.getTiktokCancellationFee());
            statsStoreDeviceType.setMeiTuanCancellationFee(item.getMeiTuanCancellationFee());
            statsDeviceStoreMap.put(this.generateKey(item), statsStoreDeviceType);
        }
        /**游戏币营业额,通用商品营业额,套票商品营业额*/
        List<StatsStoreDeviceType> statsStoreDeviceTypeOrderItemList = statsStoreDeviceTypeDao.statsStatesStoreOrderItemDeviceType(startTime, endTime);
        for (StatsStoreDeviceType item : statsStoreDeviceTypeOrderItemList) {
            StatsStoreDeviceType statsStoreDeviceType = statsDeviceStoreMap.get(this.generateKey(item));
            if (ObjectUtils.isEmpty(statsStoreDeviceType)) {
                statsStoreDeviceType = this.init(item);
            }
            statsStoreDeviceType.setTurnoverGameCoin(item.getTurnoverGameCoin());
            statsStoreDeviceType.setTurnoverGeneralCommodity(item.getTurnoverGeneralCommodity());
            statsStoreDeviceType.setTurnoverTicketPackageCommodity(item.getTurnoverTicketPackageCommodity());
            statsDeviceStoreMap.put(this.generateKey(item), statsStoreDeviceType);
        }
        /** 统计美团抖音服务费*/
        List<StatsStoreDeviceType> statsLedgerOrderList = statsStoreDeviceTypeDao.statsStatsStoreDetailByLedgerOrderDeviceType(startTime, endTime);
        for (StatsStoreDeviceType item : statsLedgerOrderList) {
            StatsStoreDeviceType statsStoreDeviceType = statsDeviceStoreMap.get(this.generateKey(item));
            if (ObjectUtils.isEmpty(statsStoreDeviceType)) {
                statsStoreDeviceType = this.init(item);
            }
            statsStoreDeviceType.setTotalTiktokMeituanServiceFee(item.getTotalTiktokMeituanServiceFee());
            statsDeviceStoreMap.put(this.generateKey(item), statsStoreDeviceType);
        }


        /**活动订单*/
        List<StatsStoreDeviceType> statsActivityOrderList = statsStoreDeviceTypeDao.statsStatsDeviceStoreByActivityOrder(startTime, endTime);
        for (StatsStoreDeviceType item : statsActivityOrderList) {

            StatsStoreDeviceType statsStoreDeviceType = statsDeviceStoreMap.get(this.generateKey(item));
            if (ObjectUtils.isEmpty(statsStoreDeviceType)) {
                statsStoreDeviceType = this.init(item);
            }

            if (StringUtils.isEmpty(statsStoreDeviceType.getTurnover())) {
                statsStoreDeviceType.setTurnover(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setTurnover(statsStoreDeviceType.getTurnover().add(item.getTurnover()));

            if (StringUtils.isEmpty(statsStoreDeviceType.getTurnoverOnLine())) {
                statsStoreDeviceType.setTurnoverOnLine(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setTurnoverOnLine(statsStoreDeviceType.getTurnoverOnLine().add(item.getTurnoverOnLine()));

            if (StringUtils.isEmpty(statsStoreDeviceType.getHandlingCharge())) {
                statsStoreDeviceType.setHandlingCharge(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setHandlingCharge(statsStoreDeviceType.getHandlingCharge().add(item.getHandlingCharge()));

            if (StringUtils.isEmpty(statsStoreDeviceType.getSettlementMoney())) {
                statsStoreDeviceType.setSettlementMoney(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setSettlementMoney(statsStoreDeviceType.getSettlementMoney().add(item.getSettlementMoney()));

            if (StringUtils.isEmpty(statsStoreDeviceType.getPlatformSettlementMoney())) {
                statsStoreDeviceType.setPlatformSettlementMoney(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setPlatformSettlementMoney(statsStoreDeviceType.getPlatformSettlementMoney().add(item.getPlatformSettlementMoney()));

            if (StringUtils.isEmpty(statsStoreDeviceType.getWxMoney())) {
                statsStoreDeviceType.setWxMoney(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setWxMoney(statsStoreDeviceType.getWxMoney().add(item.getWxMoney()));

            if (StringUtils.isEmpty(statsStoreDeviceType.getAliMoney())) {
                statsStoreDeviceType.setAliMoney(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setAliMoney(statsStoreDeviceType.getAliMoney().add(item.getAliMoney()));

            //未核销
            if (StringUtils.isEmpty(statsStoreDeviceType.getActivityUnwrittenMoney())) {
                statsStoreDeviceType.setActivityUnwrittenMoney(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setActivityUnwrittenMoney(statsStoreDeviceType.getActivityUnwrittenMoney().add(item.getActivityUnwrittenMoney()));
            //已核销
            if (StringUtils.isEmpty(statsStoreDeviceType.getActivityCancelledMoney())) {
                statsStoreDeviceType.setActivityCancelledMoney(BigDecimal.ZERO);
            }
            statsStoreDeviceType.setActivityCancelledMoney(statsStoreDeviceType.getActivityCancelledMoney().add(item.getActivityCancelledMoney()));

            statsStoreDeviceType.setSeckillMoney(item.getSeckillMoney());
            statsStoreDeviceType.setGroupBookingMoney(item.getGroupBookingMoney());
            statsStoreDeviceType.setConsumptionOfRedEnvelopesMoney(item.getConsumptionOfRedEnvelopesMoney());
            statsStoreDeviceType.setMemberOfTheStoredValueMoney(item.getMemberOfTheStoredValueMoney());

            statsDeviceStoreMap.put(this.generateKey(statsStoreDeviceType), statsStoreDeviceType);
            log.info("活动订单数据："+ JSON.toJSONString(statsStoreDeviceType));
        }



        /**
         * 分片批量插入（批量数据过大）-begin
         */
        for (StatsStoreDeviceType item : statsDeviceStoreMap.values()) {
            statsStoreDeviceTypeDao.add(item);
        }

    }

    @Override
    public void statsMonthStoreDeviceType() {
        StatsStoreDeviceTypeDto statsStoreDeviceTypeDto = new StatsStoreDeviceTypeDto();
        statsStoreDeviceTypeDto.setStartDateStr(DateTimeUtils.getPreviousMonthFirstDay());
        statsStoreDeviceTypeDto.setEndDateStr(DateTimeUtils.getPreviousMonthLastDay());
        List<StatsStoreDeviceType> statsMonthStoreDeviceType = statsStoreDeviceTypeDao.statsMonthStoreDeviceType(statsStoreDeviceTypeDto);
        statsMonthStoreDeviceType.forEach(statsDeviceStore -> {
            statsDeviceStore.setType(2);
            statsDeviceStore.setDateStr(DateTimeUtils.getPreviousMonth());
            statsStoreDeviceTypeDao.add(statsDeviceStore);
        });
    }

    /**
     * 生成key
     *
     * @param item
     * @return
     */
    private String generateKey(StatsStoreDeviceType item) {
        return item.getMchStoreId() + "_" + item.getDeviceTypeId();
    }

    private StatsStoreDeviceType init(StatsStoreDeviceType param) {
        StatsStoreDeviceType statsStoreDeviceType = new StatsStoreDeviceType();
        statsStoreDeviceType.setType(1);
        statsStoreDeviceType.setDateStr(DateTimeUtils.getYesterday());
        statsStoreDeviceType.setMchStoreId(param.getMchStoreId());
        statsStoreDeviceType.setDeviceTypeId(param.getDeviceTypeId());
        statsStoreDeviceType.setTurnover(BigDecimal.ZERO);
        statsStoreDeviceType.setHandlingCharge(BigDecimal.ZERO);
        statsStoreDeviceType.setSettlementMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setPlatformSettlementMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setWxMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setAliMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setCashMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setOutCoinTotal(0);
        statsStoreDeviceType.setOnLinePayOutCoinTotal(0);
        statsStoreDeviceType.setOffLinePayOutCoinTotal(0);
        statsStoreDeviceType.setFetchMemberOutCoinTotal(0);
        statsStoreDeviceType.setDouYinOutCoinTotal(0);
        statsStoreDeviceType.setMeiTuanOutCoinTotal(0);
        statsStoreDeviceType.setOutCoinSales(0);
        statsStoreDeviceType.setOutCoinGive(0);
        statsStoreDeviceType.setInsertCoinTotal(0);
        statsStoreDeviceType.setInsertCoinOnLine(0);
        statsStoreDeviceType.setInsertCoinOffline(0);
        statsStoreDeviceType.setConsumeRecordCount(0);
        statsStoreDeviceType.setGiftConsume(0);
        statsStoreDeviceType.setGiftCost(BigDecimal.ZERO);
        statsStoreDeviceType.setGiftOutRate(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverOnLine(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverOffline(BigDecimal.ZERO);
        statsStoreDeviceType.setSeckillMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setGroupBookingMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setConsumptionOfRedEnvelopesMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setMemberOfTheStoredValueMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setArtificialOutCoinTotal(0);
        statsStoreDeviceType.setWeiXinGroupBuyingOutCoinTotal(0);
        statsStoreDeviceType.setWeiXinSeckillOutCoinTotal(0);
        statsStoreDeviceType.setWeiXinVoucherOutCoinTotal(0);
        statsStoreDeviceType.setGroupBenefitCoinTotal(0);
        statsStoreDeviceType.setDistributeBenefitCoinTotal(0);
        statsStoreDeviceType.setGameCoinRedPacketCoinTotal(0);
        statsStoreDeviceType.setActivityCancelledMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setActivityUnwrittenMoney(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverGameCoin(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverGeneralCommodity(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverTicketPackageCommodity(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverMeiTuan(BigDecimal.ZERO);
        statsStoreDeviceType.setTurnoverDouYin(BigDecimal.ZERO);
        statsStoreDeviceType.setSelfServiceTerminalCash(BigDecimal.ZERO);
        statsStoreDeviceType.setScanningFee(BigDecimal.ZERO);
        statsStoreDeviceType.setTiktokCancellationFee(BigDecimal.ZERO);
        statsStoreDeviceType.setMeiTuanCancellationFee(BigDecimal.ZERO);
        statsStoreDeviceType.setTotalTiktokMeituanServiceFee(BigDecimal.ZERO);
        return statsStoreDeviceType;

    }
}
